#include "Stack.h"

void StackInit(Stack* st)
{
	assert(st);
	st->a = NULL;
	st->top = st->capacity = 0;
}

void StackDestory(Stack* st)
{
	assert(st);
	free(st->a);
	st->a = NULL;
	st->top = st->capacity = 0;
}
void StackPush(Stack* st, StackDate x)
{
	assert(st);
	if (st->top == st->capacity)
	{
		int newcapacity = st->capacity == 0 ? 4 : st->capacity * 2;
		StackDate* tmp = (StackDate*)realloc(st->a, newcapacity * sizeof(StackDate));
		if (tmp == NULL)
		{
			printf("relloc err\n");
			return;
		}
		st->a = tmp;
		st->capacity = newcapacity;
	}
	st->a[st->top] = x;
	st->top++;
}
void StackPop(Stack* st)
{
	assert(st);
	assert(!StackEmpty(st));
	st->top--;
}
StackDate StackTop(Stack* st)
{
	assert(st);
	assert(!StackEmpty(st));
	return st->a[st->top-1];
}
bool StackEmpty(Stack* st)
{
	assert(st);
	return st->top == 0;
}
int StackSize(Stack* st)
{
	assert(st);
	return st->top;
}
